package br.com.bolsamovel;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class CopyOfMain extends Activity {
	List<Object> items = new ArrayList<Object>();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
/*		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		ListView listView = (ListView) findViewById(R.id.listView);

		//final ArrayAdapter<Object> adp = new ArrayAdapter<Object>(this,R.layout.listlayout, items);
		
		DBHelpper db = new DBHelpper(this);
		
		final SQLiteDatabase sqDb = db.getWritableDatabase();
		
		Cursor c = getAllLines(sqDb); 
		
		final SimpleCursorAdapter adp = new SimpleCursorAdapter(this,R.layout.dblayout, c,new String[]{"_id","descricao"} , new int[]{R.id._id,R.id.descricao});

		listView.setAdapter(adp);

		final EditText edittext = (EditText) findViewById(R.id.someText);
		edittext.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// If the event is a key-down event on the "enter" button
				if ((event.getAction() == KeyEvent.ACTION_DOWN)
						&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
					// Perform action on key press
					// Toast.makeText(Main.this, edittext.getText(),
					// Toast.LENGTH_SHORT).show();
					sqDb.execSQL("insert into teste (descricao) values(?)", new Object[]{edittext.getText().toString()});
					
					adp.changeCursor(getAllLines(sqDb));
					
					//items.add(edittext.getText().toString());
					adp.notifyDataSetChanged();
					return true;
				}
				return false;
			}
		});
*/	}
	
	private Cursor getAllLines(SQLiteDatabase sqDb){
		return sqDb.rawQuery("select _id,descricao from teste", null);
	}
	
	private static class DBHelpper extends SQLiteOpenHelper{
		public DBHelpper(Context ctx){
			super(ctx, "bolsa_movel", null, 1);
		}
		
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("create table teste (_id integer primary key autoincrement, descricao);");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		}
		
	}
}